package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.entity.WxUser;
import com.sixmac.service.AccountService;
import com.sixmac.service.WxUserService;
import com.sixmac.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
@Controller
@RequestMapping(value = "backend/wxUser")
public class WxUserController extends CommonController {

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private AccountService accountService;

    @RequestMapping("index")
    public String index() {
        return "backend/微信用户列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String nickname,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<WxUser> page = wxUserService.page(nickname, pageNum, length);

        for (WxUser wxUser : page.getContent()) {
            wxUser.setAccountList(accountService.findListById(wxUser.getId(), Constant.ACCOUNT_STATUS_WeiXin));
            wxUser.setCount(wxUser.getAccountList().size());
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer id) {
        if (null != id) {
            WxUser wxUser = wxUserService.getById(id);
            wxUser.setAccountList(accountService.findListById(wxUser.getId(), Constant.ACCOUNT_STATUS_WeiXin));
            model.addAttribute("wxUser", wxUser);
        }

        return "backend/微信用户详情";
    }
}
